﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
using CefSharp.WinForms;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace wfaResClient
{ 
    public class JavascriptCsharp
    {
        DeviceControl dc = new DeviceControl();
        public static string myPath = Application.StartupPath;//程序启动路径
        public static string pagesPath = Path.Combine(myPath, "Pages");//HTML文件路径
        public static string fliePath = Path.Combine(myPath, "RecFile");//录音文件路径
        public ChromiumWebBrowser browser;
        public string url = PublicData.notebookUrl;
        public string phNumber { get; set; }
        public void setphNumber(string ph)
        {
            phNumber = ph;
        }
        public string uid()//赋值给查询页面当前登录id
        {
            return PublicData.LoginID;
        }
        /// <summary>
        /// js调用拨打电话
        /// </summary>
        public void Call(string ph)//拨号界面调用拨号
        {
                dc.Dialing(ph.Trim());
                PublicData.IPone = ph;
                PublicData.CallType = "0";
                PublicData.CreaTime = string.Format("{0:yyyy/MM/dd HH:mm:ss}", DateTime.Now);
        }
        public void Call2(string quhao)//通讯录拨号界面调用
        {
            if (quhao != "")
            {
                    PublicData.IPone = quhao.Trim() + phNumber.Trim();
                    PublicData.CallType = "0";
                    PublicData.CreaTime = string.Format("{0:yyyy/MM/dd HH:mm:ss}", DateTime.Now);
                    dc.Dialing(quhao.Trim() + phNumber.Trim());
            }
            else
            {
                PublicData.IPone = quhao.Trim() + phNumber.Trim();
                PublicData.CallState = "0";
                PublicData.CreaTime = string.Format("{0:yyyy/MM/dd HH:mm:ss}", DateTime.Now);
                dc.Dialing(phNumber.Trim());
            }
            
        }
        /// <summary>
        /// 挂断
        /// </summary>
        public void hangUp()
        {
            PublicData.CallState = "0";
            PublicData.Updatetime = string.Format("{0:yyyy/MM/dd HH:mm:ss}", DateTime.Now);
            dc.HangUp();//挂断电话
        }
        /// <summary>
        /// js调用接听电话
        /// </summary>
        public void Answer()
        {
            PublicData.CallType = "1";
            PublicData.CallState = "1";
            PublicData.CreaTime = string.Format("{0:yyyy/MM/dd HH:mm:ss}", DateTime.Now);
            dc.AnswerPhone();
        }
        /// <summary>
        /// 重新载入页面
        /// </summary>
        public void laction()
        {
            browser.Load(GetPagePath("index.html"));
        }
        /// <summary>
        /// 重新载入页面
        /// </summary>
        public void lactioncall()
        {
            browser.Load(GetPagePath("dial.html"));
        }
        /// <summary>
        /// 重新载入首页
        /// </summary>
        public void lactionhome()
        {
            browser.Load(url);
        }
        
        /// <summary>
        /// js调用电话录音
        /// </summary>
        public void JsRecording(string PhName)
        {
            PublicData.IsRecding = true;
            dc.CallRecording(PhName);//调用录音
        }
        public string GetPagePath(string pageName)//获得本地网页路径
        {
            return Path.Combine(pagesPath, pageName);
        }
        public void ChangePage(string pageName)//加载当前页面
        {
            browser.Load(GetPagePath(pageName));
        }
        /// <summary>
        /// 用户设置默认号码
        /// </summary>
        public string DefaultNumber()
        {
            string defaultNumber = Properties.Settings.Default.DefaultCallingNumber;
            return defaultNumber;
        }
        public void OnFrameLoadEnd(object seder, FrameLoadEndEventArgs e)//页面加载完成注册js
        {
            if (e.Frame.IsMain)
            {
                //if (showId)
                //{
                //    //browser.ExecuteScriptAsync(@"window.open('login.html', '','width=530px,height=360px')");
                //    //showId = false;
                //}
            }
        }
        public void OnIsBrowserInitializedChanged(object sender, IsBrowserInitializedChangedEventArgs args)//页面开始加载注册js
        {
            if (args.IsBrowserInitialized)
            {
                //browser.ExecuteScriptAsync(@"
                //document.getElementById('address').onclick=function(){JCall.laction();}");
                //browser.ExecuteScriptAsync(@"
                //document.getElementById('callHome').onclick=function(){JCall.lactioncall();}");
            }
        }

    }
}
